﻿using ImageManager.Common;
using ImageManager.DBAccess;
using ImageManager.Model;
using ImageManager.MyExcep;

namespace ImageManager.ImageService
{
    public class UserServiceProvider : BaseServiceProvider<UserModel>
    {
        public UserModel VerifyPassword(string user, string pwd)
        {
            var _pwd = Utils.Md5(pwd);
            var qry = qryInstance.Where(CondCollection.True().Eq("password", _pwd).Eq(nameof(UserModel.UserCode), user));
            return qry.First()!;
        }
        public string ResetPassword(string userCode, string? password = null)
        {
            if (String.IsNullOrEmpty(password))
                password = Utils.MakePassword();
            udpInstance.ForceUpdateCol(nameof(UserModel.UserCode), "password", userCode, Utils.Md5(password));
            return password;
        }
        public OnlineUser MakeOnLineUser(string uid)
        {
            var result = ServiceLoader.GetService<OnlineUser>();
            result.UserId = uid;
            SetupOnLineUser(result);
            return result;
        }
        public void SetupOnLineUser(OnlineUser result)
        {
            if (string.IsNullOrEmpty(result.UserId))
            {
                throw new MyEmptyOrNullDataException("需要先设置UserId才能使用这个方法");
            }
            var user = Get(result.UserId);
            if (!user.Avaliable.Val)
            {
                throw new MyUserDisableException(user.UserCode);
            }
            result.UserName = user.UserName;
            result.UserId = user.Pk;
            result.UserCode = user.UserCode;
            result.IsRoot = user.Pk == Const.SUPER_USER;
            SetupUserRight(result);
            if (string.IsNullOrWhiteSpace(result.Token))
            {
                DateTime dt;
                result.Token = TokenManager.MakeToken4User(result.UserId, out dt);
            }
        }
        private void SetupUserRight(OnlineUser onlineUser)
        {
            var modService = ServiceLoader.GetService<ModServiceProvider>();
            var mods = modService.ListByMode(MyModAccessMode.NONE);
            if (onlineUser.IsRoot)
            {
                mods.AddRange(modService.ListByMode(MyModAccessMode.ROOT));
                mods.AddRange(modService.ListByMode(MyModAccessMode.ACL));
            }

            List<UserRightDetail> result = new List<UserRightDetail>();
            foreach (var item in mods)
            {
                result.Add(new UserRightDetail()
                {
                    Approve = true,
                    Mod = item.Pk,
                    Write = true,
                    Read = true,
                    Remove = true,
                });
            }
            var urSP = ServiceLoader.GetService<UserRightServiceProvider>();
            var urMods = urSP.ListByUser(onlineUser.UserId);
            foreach (var item in urMods)
            {
                if (result.Exists(i => i.Mod == item.Mod)) continue;
                result.Add(new UserRightDetail()
                {
                    Approve = item.Approve.Val,
                    Mod = item.Mod,
                    Write = item.Write.Val,
                    Read = item.Read.Val,
                    Remove = item.Remove.Val,
                });
            }
            onlineUser.Detail = result;
        }
    }
}
